/*
 * Created on 2014年10月13日 上午11:17:52
 */
package utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class JsonUtil {

    private static Log logger = LogFactory.getLog(JsonUtil.class);
    private static Gson gson = buildGson();

    /**
     * JSON字符串转成bean对象
     *
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T str2Bean(String json, Class<T> clazz) {
        Gson gson = buildGson();
//		logger.debug("json0 = " + json);
        return gson.fromJson(json, clazz);
    }

    /**
     * JSON字符串转成bean对象
     *
     * @param json
     * @return
     */
    public static <T> T str2Bean(String json, Type typeOfT) {
        Gson gson = buildGson();
//		logger.debug("json1 = " + json);
        return gson.fromJson(json, typeOfT);
    }

    /**
     * JSON字符串转成Map对象
     *
     * @param json
     * @return
     */
    public static Map<String, Object> str2Map(String json) {

//		logger.debug("json2 = " + json);
        return gson.fromJson(json, new TypeToken<HashMap<String, Object>>() {
        }.getType());
    }

    private static Gson buildGson() {
//		return new GsonBuilder().registerTypeAdapter(new TypeToken<Map<String, Object>>() {}.getType(), new JsonDeserializer<Map<String, Object>>() {
//			public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
//					throws JsonParseException {
//				Map<String, Object> resultMap = new HashMap<String, Object>();
//				JsonObject jsonObject = json.getAsJsonObject();
//				Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
//				for (Map.Entry<String, JsonElement> entry : entrySet) {
//					resultMap.put(entry.getKey(), entry.getValue());
//				}
//				return resultMap;
//			}
//		}).create();
        return new GsonBuilder().disableHtmlEscaping().create();
    }

    /**
     * 对象转成JSON字符串
     *
     * @param obj
     * @return
     */
    public static String bean2Str(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    /**
     * json字符串格式化
     * @param jsonString
     * @return
     */
    public static String jsonFormatter(String jsonString){
        com.google.gson.Gson gson = new com.google.gson.GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(jsonString);
        String prettyJsonString = gson.toJson(je);
        return prettyJsonString;
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        long a = 1000000000000000000L;
        Object c = "c";
        map.put("a", a);
        map.put("b", "b");
        map.put("c", c);

        String str = JsonUtil.bean2Str(map);

        System.out.println(str);

        Map<String, Object> map2 = JsonUtil.str2Map(str);

        System.out.println(map2.toString());
        System.out.println(map2.get("a"));
        System.out.println(map2.get("b"));


        //Java中把"替换为/"
        String message = "\"book\"";
        System.out.println(message);
        //需要五个
        message = message.replaceAll("\"", "");
        System.out.println(message);
    }


}
